Range Function


In [3]:
x = range(1,10)

In [4]:
x


Out[4]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [7]:
type(x)


Out[7]:
list

In [5]:
for items in x:
    print(items)


1
2
3
4
5
6
7
8
9

In [6]:
type(items)


Out[6]:
int

In [8]:
y =  xrange(1,10)

In [9]:
type(y)


Out[9]:
xrange

In [10]:
y


Out[10]:
xrange(1, 10)

In [12]:
for items in y:
    print(items)


1
2
3
4
5
6
7
8
9

List Comprehension


In [3]:
l = []

In [4]:
for item in "String":
    l.append(item)
l


Out[4]:
['S', 't', 'r', 'i', 'n', 'g']

In [6]:
lst_sqrs  = [sqrs**2 for sqrs in range(0,10)]
lst_sqrs


Out[6]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [9]:
evenos = [evn for evn in range(1,103) if evn%2 == 0]
evenos


Out[9]:
[2,
 4,
 6,
 8,
 10,
 12,
 14,
 16,
 18,
 20,
 22,
 24,
 26,
 28,
 30,
 32,
 34,
 36,
 38,
 40,
 42,
 44,
 46,
 48,
 50,
 52,
 54,
 56,
 58,
 60,
 62,
 64,
 66,
 68,
 70,
 72,
 74,
 76,
 78,
 80,
 82,
 84,
 86,
 88,
 90,
 92,
 94,
 96,
 98,
 100,
 102]

In [11]:
celcisus  = [0,32,40,45,49]
farenheight = [temp for temp in celcisus]
farenheight


Out[11]:
[0, 32, 40, 45, 49]

In [14]:
farenheight = [(temp*(9/5.0)+32)  for temp in celcisus]
farenheight


Out[14]:
[32.0, 89.6, 104.0, 113.0, 120.2]

In [17]:
lst = [item**2 for item in [item**2 for item in range(11)]]
lst


Out[17]:
[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]

In [18]:
lst = [item for item in range(51) if item%3 == 0]
lst


Out[18]:
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

In [ ]: